package com.joy.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joy.commonutils.R;
import com.joy.eduservice.entity.EduTeacher;
import com.joy.eduservice.entity.vo.TeacherQuery;
import com.joy.eduservice.service.impl.EduTeacherServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-27
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherServiceImpl eduTeacherService;

    //查询讲师所有的数据
    //restful风格
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        //调用service方法来实现查询全部
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    //进行逻辑删除，逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //分页查询讲师的方法
    //current:当前页
    //limit:每页记录数
    @GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        //此句是分页方法，指定了一些分页参数，以及指定分完页将数据放到哪,查询出了总页数，将每一页封装到此对象里面的list集合
        eduTeacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
    }

    //4、条件查询带分页的方法
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){//加了@RequestBody需要使用Post提交方式，Get提交方式不行,false表示对象可以为空
        //创建Page对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);
        //创建封装条件对象
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //条件组合查询，判断条件值是否为空，如果不为空，则拼接上
        if(!StringUtils.isEmpty(name)){
            eduTeacherQueryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level.toString())){
            eduTeacherQueryWrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            eduTeacherQueryWrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            eduTeacherQueryWrapper.le("gmt_modified",end);
        }
        eduTeacherService.page(eduTeacherPage,eduTeacherQueryWrapper);
        long total = eduTeacherPage.getTotal();
        List<EduTeacher> records = eduTeacherPage.getRecords();


        return R.ok().data("total",total).data("rows",records);

    }

    //添加讲师接口的方法
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据讲师Id进行查询
    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    //修改讲师的方法
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

